package com.onlyoffice.demo.onlyoffice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onlyoffice.demo.onlyoffice.domain.File;
import com.onlyoffice.demo.onlyoffice.domain.R;
import com.onlyoffice.demo.onlyoffice.domain.RenameFileReq;
import com.onlyoffice.demo.onlyoffice.service.IFileService;
import com.onlyoffice.demo.onlyoffice.service.impl.FileCallbackServiceImpl;
import com.onlyoffice.model.documenteditor.Callback;
import com.onlyoffice.model.documenteditor.Config;
import com.onlyoffice.model.documenteditor.config.editorconfig.Mode;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件Controller
 *
 * @author ZWX
 * @date 2025-07-09
 */
@Slf4j(topic = "file")
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {
    private final IFileService fileService;
    private final FileCallbackServiceImpl callbackService;

    /**
     * 获取文件列表-分页
     *
     * @param pageNum  当前页数
     * @param pageSize 每页记录数
     * @param file     查询条件
     * @return 文件列表-分页
     */
    @GetMapping
    public R<Page<File>> getFileList(Integer pageNum, Integer pageSize, File file) {
        Page<File> list = fileService.page(new Page<>(pageNum, pageSize));
        return R.ok(list);
    }

    /**
     * 上传文件
     *
     * @param file 文件
     * @return 结果
     */
    @PostMapping("/upload")
    public R<Void> uploadFile(@RequestBody MultipartFile file) {
        if (fileService.uploadFile(file)) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 删除文件
     *
     * @param fileId 文件Id
     * @return 结果
     */
    @DeleteMapping("/{fileId}")
    public R<Void> deleteFile(@PathVariable("fileId") Long fileId) {
        if (fileService.deleteFile(fileId)) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 重命名文件
     *
     * @return 结果
     */
    @PutMapping("/rename")
    public R<Void> renameFile(@RequestBody RenameFileReq req) {
        if (fileService.renameFile(req)) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 获取Onlyoffice配置信息
     *
     * @param fileId 文件ID
     * @param mode   模式
     * @param userId 用户Id
     * @return Onlyoffice配置信息
     */
    @GetMapping("/config")
    public R<Config> getConfig(Long fileId, Mode mode, @RequestParam(value = "userId", defaultValue = "1") Long userId) {
        return R.ok(fileService.getOnlyOfficeConfig(fileId, mode, userId));
    }

    /**
     * 文档编辑回调方法
     *
     * @param request  请求参数
     * @param callback 回调参数
     * @param fileId   文件Id
     * @return 回调结果
     */
    @PostMapping("/callback")
    public String callback(final HttpServletRequest request, @RequestBody Callback callback, @RequestParam Long fileId) {
        log.info("callback status = {}", callback.getStatus());
        // 校验callback参数
        try {
            String authorization = request.getHeader("Authorization");
            callback = callbackService.verifyCallback(callback, authorization);
            callbackService.processCallback(callback, fileId.toString());
        } catch (Exception e) {
            log.error("", e);
        }
        return "{\"error\": \"0\"}";
    }
}
